package com.google.gwt.mobile.phonegap.api;

import com.google.gwt.core.client.JavaScriptObject;

/**
 * The Class Media.
 */
public class Media extends JavaScriptObject {

  protected Media() {
  }

  /**
   * New instance.
   * 
   * @param src the src
   * @param callback the callback
   * @return the media
   */
  public static native Media newInstance(String src, Callback callback) /*-{
		return new $wnd.Media(
				src,
				function() {
					callback.@com.google.gwt.mobile.phonegap.api.Media.Callback::onSuccess()();
				},
				function(error) {
					callback.@com.google.gwt.mobile.phonegap.api.Media.Callback::onError(Lcom/google/gwt/mobile/phonegap/api/Media$MediaError;)(error);
				});
  }-*/;

  /**
   * Play.
   */
  public native final void play() /*-{
		this.play();
  }-*/;

  /**
   * Pause.
   */
  public native final void pause() /*-{
		this.pause();
  }-*/;

  /**
   * Stop.
   */
  public native final void stop() /*-{
		this.stop();
  }-*/;

  /**
   * Release.
   */
  public native final void release() /*-{
		if ($wnd.navigator.userAgent.indexOf("Android") != -1) {
			this.release();
		}
  }-*/;

  /**
   * Start record.
   */
  public native final void startRecord() /*-{
		if (typeof this.startRecord == 'function') {
			this.startRecord();
		} else {
			this.startAudioRecord();
		}
  }-*/;

  /**
   * Stop record.
   */
  public native final void stopRecord() /*-{
		if (typeof this.stopRecord == 'function') {
			this.stopRecord();
		} else {
			this.stopAudioRecord();
		}
  }-*/;

  /**
   * Gets the current position.
   * 
   * @param callback the callback
   * @return the current position
   */
  public native final void getCurrentPosition(PositionCallback callback) /*-{
		this
				.getCurrentPosition(
						function(position) {
							callback.@com.google.gwt.mobile.phonegap.api.Media.PositionCallback::onSuccess(I)(position);
						},
						function(error) {
							callback.@com.google.gwt.mobile.phonegap.api.Media.PositionCallback::onError(Lcom/google/gwt/mobile/phonegap/api/Media$MediaError;)(error);
						});
  }-*/;

  /**
   * Gets the duration.
   * 
   * @return the duration
   */
  public native final int getDuration() /*-{
		return this.getDuration();
  }-*/;

  /**
   * The Interface Callback.
   */
  public interface Callback {
    void onSuccess();

    void onError(MediaError error);
  }

  /**
   * The Interface PositionCallback.
   */
  public interface PositionCallback {
    void onSuccess(int position);

    void onError(MediaError error);
  }

  /**
   * The Enum MediaErrorCode.
   */
  public enum MediaErrorCode {
    MEDIA_ERR_ABORTED, MEDIA_ERR_NETWORK, MEDIA_ERR_DECODE, MEDIA_ERR_NONE_SUPPORTED
  };

  /**
   * The Class MediaError.
   */
  public static class MediaError extends JavaScriptObject {

    protected MediaError() {
    }

    // TODO: PhoneGap Android version does not follow its doc.
    /**
     * Gets the code.
     * 
     * @return the code
     */
    public final MediaErrorCode getCode() {
      return MediaErrorCode.values()[getCodeNative()];
    }

    private native final int getCodeNative() /*-{
			return this.code;
    }-*/;

    /**
     * Gets the message.
     * 
     * @return the message
     */
    public native final String getMessage() /*-{
			return this.message;
    }-*/;

  }

}
